package com.example.controller;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.example.entity.Mycoupon;
import com.example.mapper.MycouponMapper;
import com.example.service.IMycouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Controller
@RequestMapping
@CrossOrigin(origins = "*") // 允许所有来源的跨域请求
public class thymeleafController {



    @Autowired
private IMycouponService mycouponService;


    @Autowired
    private MyCouponController myCouponController;


    @GetMapping("/hello")
    public String hello(Model model){
        model.addAttribute("message","helloworld");
        Mycoupon mycoupon=new Mycoupon();
        mycoupon.setMycouponName("Discount Coupon");
        mycoupon.setMycouponPrice(new BigDecimal("10.00"));
        mycoupon.setMycouponNumber("1234567890");
        mycoupon.setCustomerId(1);
        mycoupon.setCustomerName("John Doe");
        mycoupon.setMycouponCreateTime(LocalDateTime.now());
        mycoupon.setMycouponStartTime(LocalDateTime.of(2024, 7, 1, 10, 0));
        mycoupon.setMycouponEndTime(LocalDateTime.of(2024, 7, 31, 23, 59));
        mycoupon.setMycouponStatus(1); // Assuming 1 means active
        model.addAttribute("mycoupon", mycoupon);
        return "hello";

    }





    @GetMapping("/showCoupons")
    public String showAllCoupons(Model model) {
        List<Mycoupon> coupons = mycouponService.list(); // 获取所有优惠券列表

        model.addAttribute("coupons", coupons); // 将优惠券列表添加到模型中
        return "show"; // 返回模板名称，Thymeleaf 将渲染 show.html
    }

    @GetMapping("/mallHomePage")
    public String showmallHomePage(Model model) {
        List<Mycoupon> coupons = mycouponService.list(); // 获取所有优惠券列表
        model.addAttribute("coupons", coupons); // 将优惠券列表添加到模型中
        return "mallHome";
    }
    @GetMapping("/myCoupons")
    public String showmyCoupons(Model model) {
        List<Mycoupon> coupons = mycouponService.list(); // 获取所有优惠券列表
        model.addAttribute("coupons", coupons); // 将优惠券列表添加到模型中
        return "myCoupons";
    }
    @GetMapping("/profileManagement")
    public String showprofileManagement(Model model) {
        List<Mycoupon> coupons = mycouponService.list(); // 获取所有优惠券列表
        model.addAttribute("coupons", coupons); // 将优惠券列表添加到模型中
        return "profile";
    }


    @GetMapping("/projectImage")
    public String showProjectImages(Model model) {
        // 获取图片文件夹中的所有文件路径
        File folder = new File("C:\\Users\\24304\\Desktop\\sping\\springboot_demo\\src\\main\\resources\\static\\ProjectImage");
        File[] listOfFiles = folder.listFiles();


        List<String> imageUrls = new ArrayList<>();
        if (listOfFiles != null) {
            for (File file : listOfFiles) {
                if (file.isFile()) {
                    // 只处理图片文件，可以根据实际需要调整条件
                    if (file.getName().endsWith(".jpg") || file.getName().endsWith(".png")) {
                        // 构造图片的URL，这里假设是本地文件系统的相对路径
                        String imageUrl = "static/ProjectImage/" + file.getName();
                        System.out.println(file.getName());
                        System.out.println(imageUrl);
                        imageUrls.add(imageUrl);
                    }
                }
            }
        }

        model.addAttribute("imageUrls", imageUrls);
        return "ProjectImage"; // 返回Thymeleaf模板名称
    }














    @GetMapping("/showCouponsPaged")
    public String showAllCouponsPaged(Model model,
                                      @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                                      @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        // 分页获取优惠券列表
        List<Mycoupon> coupons = mycouponService.getCouponsPaged(pageNum, pageSize);
        model.addAttribute("coupons", coupons); // 将分页查询结果添加到模型中

        // 添加分页相关信息到模型中
        model.addAttribute("currentPage", pageNum);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("totalItems", mycouponService.countTotalCoupons()); // 总条目数，用于计算总页数
        model.addAttribute("totalPages", calculateTotalPages(pageSize)); // 计算总页数

        return "show"; // 返回模板名称，Thymeleaf 将渲染 show.html



    }

    // 计算总页数
    private int calculateTotalPages(int pageSize) {
        int totalItems = mycouponService.countTotalCoupons(); // 总条目数
        return (int) Math.ceil((double) totalItems / pageSize);
    }











    @ResponseBody
    @DeleteMapping("/delete/{id}")
    public String deleteCoupon(@PathVariable("id")Integer mycouponId){


        // 调用Service层进行删除操作
        boolean deleted = myCouponController.deleteCoupon(mycouponId);
        if (deleted) {
            return "ok"; // 返回成功消息给前端
        } else {
            return "error"; // 或者返回错误消息给前端
        }

    }



/*    @ResponseBody
    @PostMapping("/edit")
    public String editCoupon(@RequestBody Mycoupon mycoupon){


        boolean updated = myCouponController.updateCoupon(mycoupon);
        if (updated) {
            System.out.println("Coupon更新成功");
            return "ok"; // 返回成功消息给前端

        } else {
            System.out.println("Coupon更新失败");
            return "error"; // 或者返回错误消息给前端
        }

    }*/






    @PostMapping("/edit")
    // @ResponseBody
    public String editCoupon(
            @RequestParam("mycouponId") Integer mycouponId,
            @RequestParam("mycouponNumber") String mycouponNumber,
            @RequestParam("customerId") Integer customerId,
            @RequestParam("customerName") String customerName,
            @RequestParam("mycouponName") String mycouponName,
            @RequestParam("mycouponPrice") BigDecimal mycouponPrice,
            @RequestParam("mycouponStatus") int mycouponStatus,Model model) {

        Mycoupon mycoupon = new Mycoupon();
        mycoupon.setMycouponId(mycouponId);
        mycoupon.setMycouponNumber(mycouponNumber);
        mycoupon.setCustomerId(customerId);
        mycoupon.setCustomerName(customerName);
        mycoupon.setMycouponName(mycouponName);
        mycoupon.setMycouponPrice(mycouponPrice);
        mycoupon.setMycouponStatus(mycouponStatus);

        boolean updated = myCouponController.updateCoupon(mycoupon);
        if (updated) {
            System.out.println("Coupon更新成功");





            List<Mycoupon> coupons = mycouponService.list(); // 获取所有优惠券列表
            model.addAttribute("coupons", coupons); // 将优惠券列表添加到模型中
            return "show"; // 返回模板名称，Thymeleaf 将渲染 show.html

        } else {
            System.out.println("Coupon更新失败");
            return "error"; // 或者返回错误消息给前端
        }






    }

















    @PostMapping("/add")
    public String addCoupon(
            @RequestParam("mycouponNumber") String mycouponNumber,
            @RequestParam("customerId") Integer customerId,
            @RequestParam("customerName") String customerName,
            @RequestParam("mycouponName") String mycouponName,
            @RequestParam("mycouponPrice") BigDecimal mycouponPrice,
            @RequestParam("mycouponStatus") int mycouponStatus,
            Model model) {

        Mycoupon newCoupon = new Mycoupon();
        newCoupon.setMycouponNumber(mycouponNumber);
        // newCoupon.setCustomerId(customerId);
        newCoupon.setCustomerName(customerName);
        newCoupon.setMycouponName(mycouponName);
        newCoupon.setMycouponPrice(mycouponPrice);
        newCoupon.setMycouponStatus(mycouponStatus);


        boolean added = myCouponController.addCoupon(newCoupon); // 假设这里有一个方法来添加优惠券

        if (added) {
            System.out.println("优惠券添加成功");

            List<Mycoupon> coupons = mycouponService.list(); // 获取所有优惠券列表
            model.addAttribute("coupons", coupons); // 将优惠券列表添加到模型中
            return "show"; // 返回模板名称，Thymeleaf 将渲染 show.html
        } else {
            System.out.println("优惠券添加失败");
            return "error"; // 或者返回错误消息给前端
        }
    }







    @GetMapping("/search")
    @ResponseBody
    public ResponseEntity<List<Mycoupon>>  searchCoupons(@RequestParam("q") String query, Model model) {
        List<Mycoupon> searchResults = mycouponService.searchCoupons(query); // 假设有一个方法来根据关键词查询优惠券


        if (searchResults != null && !searchResults.isEmpty()) {

            return ResponseEntity.ok(searchResults);

        } else {

            // 可以返回一个空的列表或者包含错误信息的响应

            return ResponseEntity.ok(Collections.emptyList());

        }





    }








}
